package com.test.lol;

import android.content.*;
import android.graphics.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import com.google.gson.*;
import java.io.*;
import java.net.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;


public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private DBResponseObject dbResponseObject; // contains response from database -> server -> this
    public static ImageObject[] io; 		// contains information about an image
    private String latitude;
    private String longitude;
    

    public ImageAdapter(Context c, String lat, String lon) {
    	
    	latitude = lat;
    	longitude = lon;
        mContext = c;
        
        getImages();
    }
    
    public void getImages(){
    		
    	try {
    		BasicResponseHandler br = new BasicResponseHandler();
        	HttpClient client = new DefaultHttpClient();
        	HttpGet get = new HttpGet(Common.baseUrl+latitude+"/"+longitude);
        	Log.v(this.getClass().getName(), Common.baseUrl+"/"+latitude+"/"+longitude);
        	String response;
        	
			response = client.execute(get, br);		
			
		    Gson g = new Gson();
		    	    
		    try{
		    	dbResponseObject = g.fromJson(response, DBResponseObject.class);	
		    }
		    catch(Exception e){
		    	io = g.fromJson(response, ImageObject[].class);
		    }
		    
		    if(dbResponseObject != null){
		    	Toast.makeText(mContext, " "+dbResponseObject.getMessage(), Toast.LENGTH_LONG).show();  
		    }
		} catch (Exception e) {
			Log.e(this.getClass().getName(), "Error:" + e.getMessage());
		}   
    }

    public int getCount() {
    	
    	if(io == null){
    		return 0;
    	}
        return io.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

 
    /*
     * create a new ImageView for each item referenced by the Adapter
     * @Return imageView
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        
        try {
        	
        	if(io == null){
        		return null;
        	}
		
			Log.v(this.getClass().getName(), "URL: " + io[position].getThumbnailPath());
        	
			URL aURL = new URL(io[position].getThumbnailPath());			
			URLConnection conn = aURL.openConnection();		
			conn.connect();
			
			InputStream is = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			Bitmap bm = BitmapFactory.decodeStream(bis);
			
			bis.close();
			is.close();

			imageView.setImageBitmap(bm);
			
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(this.getClass().getName(), "getView exception: " + e.getMessage());
		}
      
        return imageView;
    }	
}
